<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    okText="确定"
    cancelText="关闭">
    
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
      
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="姓名">
          <!-- <a-select show-search :filter-option="filterOption" placeholder="请选择姓名" v-decorator="['PersonalId', validatorRules.persionalrelationid]" >
            <a-select-option value="">请选择</a-select-option>
            <a-select-option :value="item.PersonalInfoId" v-for="(item,index) in personData">{{item.PersonalName}}</a-select-option>
          </a-select> -->
          <a-input placeholder="请选择姓名" v-show v-decorator="['PersonalId', validatorRules.persionalrelationid]"></a-input>
          <a-input placeholder="请选择姓名" @click="showCommonUserModal" v-decorator="['Personal_PersonalName',{}]"></a-input>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="关系人姓名">
          <a-input placeholder="请选择关系人" v-show v-decorator="['RelationPersonalId', validatorRules.personalrelationid]"></a-input>
          <a-input placeholder="请选择关系人" @click="showCommonUserModalRelation" v-decorator="['RelationPersonal_PersonalName',{}]"></a-input>
          <!-- <a-select placeholder="请选择关系人姓名" v-decorator="['RelationPersonalId', validatorRules.personalrelationid]" >
            <a-select-option value="">请选择</a-select-option>
            <a-select-option :value="item.PersonalInfoId" v-for="(item,index) in personData">{{item.PersonalName}}</a-select-option>
          </a-select> -->
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="关系类型">
          <a-select placeholder="请输入关系类型" v-decorator="['RelationTypeId', validatorRules.relationtypeid]" >
            <a-select-option value="">请选择</a-select-option>
            <a-select-option :value="item.BaseInfoId" v-for="(item,index) in relationData">{{item.Value}}</a-select-option>
          </a-select>
        </a-form-item>
		  
      </a-form>
      <commonModal
      modalTitle="人员选择"
      rowKey="PersonalInfoId"
      ref="commonModalUser"
      @getCommonModalData="commonModalDataUser"
      :columns="tableColumnsUser"
      apiUrl="/personalInfo/queryRows"
      :searchArr="searchArrUser"></commonModal>
      <commonModal
      modalTitle="人员选择"
      rowKey="PersonalInfoId"
      ref="commonModalUserRelation"
      @getCommonModalData="commonModalDataUserRelation"
      :columns="tableColumnsUser"
      apiUrl="/personalInfo/queryRows"
      :searchArr="searchArrUser"></commonModal>
    </a-spin>
  </a-modal>
</template>

<script>
  import { httpAction,getAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import moment from "moment"
  import commonModal from '@/components/commonModal'
  export default {
    name: "PersonalRelationModal",
    components:{
      commonModal
    },
    data () {
      return {
        tableColumnsUser:[
          { title: '姓名', dataIndex: 'PersonalName' },
          { title: '性别', dataIndex: 'Gender_Value' },
        ],
        searchArrUser:[
          { label: '姓名', key: 'PersonalName' },
        ],
        relationData:[{
          id:'135',
          name:"家人"
        },{
          id:'136',
          name:"同学"
        },{
          id:'137',
          name:"同事"
        },{
          id:'138',
          name:"校友"
        },{
          id:'139',
          name:"老乡"
        },{
          id:'140',
          name:"其它"
        }],
        title:"操作",
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        personData:[],
        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules:{
        persionalrelationid:{rules: [{ required: true, message: '请选择姓名!' }]},
        personalid:{rules: [{ required: true, message: '请输入个人Id!' }]},
        personalrelationid:{rules: [{ required: true, message: '请选择姓名!' }]},
        relationpersonalid:{rules: [{ required: true, message: '请输入关系人Id!' }]},
        relationtypeid:{rules: [{ required: true, message: '请输入关系类型!' }]},
        updatebyid:{rules: [{ required: true, message: '请输入最近更新人!' }]},
        ts:{rules: [{ required: true, message: '请输入最近更新日期!' }]},
        datastatus:{rules: [{ required: true, message: '请输入状态!' }]},
        },
        url: {
          add: "/personalRelation/add",
          edit: "/personalRelation/update",
          baseList:"/baseInfo/queryRows",
        },
      }
    },
    created () {
      this.getBaseData();
    },
    methods: {
      showCommonUserModal () {
        this.$refs.commonModalUser.open()
      },
      commonModalDataUser(val){
        this.form.setFieldsValue({
          PersonalId:val[0].PersonalInfoId,
          Personal_PersonalName:val[0].PersonalName
        })
      },
      showCommonUserModalRelation () {
        this.$refs.commonModalUserRelation.open()
      },
      commonModalDataUserRelation(val){
        this.form.setFieldsValue({
          RelationPersonalId:val[0].PersonalInfoId,
          RelationPersonal_PersonalName:val[0].PersonalName
        })
      },
      filterOption(input, option) {
        return (
          option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
        );
      },
      getBaseData(){
        this.$http.postAction(this.url.baseList,{pageNo:1,pageSize:100,selectFields:[],conditions:[{
              "field":"TypeCode",
              "operator":"=",
              "value":'RelationType'
            }]}).then((res)=>{
          if(res.status == 1){
            this.relationData = res.data.rows;
          }
        })
      },
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.model.RelationTypeId = this.model.RelationType;
        this.model.PersonalId = this.model.Personal;
        this.model.RelationPersonalId = this.model.RelationPersonal;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'RelationTypeId','PersonalId','RelationPersonalId', 'Personal_PersonalName', 'RelationPersonal_PersonalName'))
		  //时间格式化
        });

      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.PersonalRelationId){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            //时间格式化
            
            console.log(formData)
            this.$http.postAction(httpurl,formData).then((res)=>{
              if(res.status == 1){
                that.$message.success('提交成功');
                if(method == 'post'){
                  this.form.resetFields();
                  this.model = {};
                }else{
                  that.close();
                }
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              // that.close();
            })



          }
        })
      },
      handleCancel () {
        this.close()
      },


    }
  }
</script>

<style lang="less" scoped>

</style>